package com.vcms.view.simulation.uicomponent.display;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import com.vcms.view.simulation.uifactory.*;

/**
 * @author Xia Xi
 * @version 1.1
 */

/**
 * This class represents the update-able text field next to the fixed label. 
 * 
 * we modify this to illustrate of the abstract factory design pattern.
 * factory getInstance to create AWTFactory Singleton. 
 * if we want to change to other factories, can just set default factory inside VMCSUIFactory.java
 */

@SuppressWarnings("serial")
public class LabelledDisplay {

	public final static int DEFAULT = 5;
	public final static char FLOW = 'a';
	public final static char GRID = 'b';

	private VMCSAbstractTextField value;
	private VMCSAbstractLabel label;
	private VMCSAbstractPanel panel;
	

	public LabelledDisplay(String labelText, int length, char layout) {
		panel = VMCSUIFactory.getInstance().createPanel();

		if (layout == FLOW)
			panel.setLayout(new FlowLayout(FlowLayout.CENTER));
		else if (layout == GRID)
			panel.setLayout(new GridLayout(1, 2));

		label = VMCSUIFactory.getInstance().createLabel();
		
		label.setAlignment(java.awt.Label.RIGHT);
		label.setText(labelText);
		
		VMCSAbstractPanel tp = VMCSUIFactory.getInstance().createPanel();
		
		tp.setLayout(new FlowLayout());
		
		value = VMCSUIFactory.getInstance().createTextField();
	
		value.setColumns(length);


		tp.add(value);

		panel.add(label);
		panel.add(tp);

	}

	public void addListener(VMCSEventHandler listener) {

		value.addEventHandler(listener);
	}

	public void setEditable(boolean v) {
		value.setEditable(v);
		if (v == false) {
			value.setBackground(java.awt.Color.blue.getRed(),java.awt.Color.blue.getGreen(),java.awt.Color.blue.getBlue());
		}
	}

	public void setValue(String vl) {
		value.setText(vl);
	}

	public void setValue(int vl) {
		String svl;

		svl = String.valueOf(vl);

		value.setText(svl);
	}

	public void setEchoChar(char c) {
		value.setEchoChar(c);
	}

	public void setActive(boolean st) {
		value.setEnabled(st);
		label.setEnabled(st);
		value.setEditable(st);
	}

	public void setTextBackground(java.awt.Color c) {
		value.setBackground(c.getRed(),c.getGreen(),c.getBlue());
	}

	public void setTextForeground(java.awt.Color c) {
		value.setForeground(c.getRed(),c.getGreen(),c.getBlue());
	}
	
	public VMCSAbstractPanel getLabelledDisplay() {
		return panel;
	}

}